﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Threading;
using Workbench.Models;
using Workbench.Operations;
using Workbench.Shared;

namespace WorkbenchTests
{
    public static class TestUtility
    {
        [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public static void DoEvents()
        {
            var frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
            Thread.Sleep(200);
            frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }

        private static object ExitFrame(object frame)
        {
            ((DispatcherFrame)frame).Continue = false;
            return null;
        }

        public static string GetTempProjectPath()
        {
            string tempPath = Path.GetTempPath();
            return Path.ChangeExtension(Path.Combine(tempPath, Guid.NewGuid().ToString()), "wbdb");
        }

        public static void CreateAndInitializeTempDatabase(string projectName = "Test project")
        {
            var projectPath = TestUtility.GetTempProjectPath();
            ProjectOperations.CreateAndOpenProject(projectPath, projectName);
        }

        public static string CreateTempFileWithText(string text, string filename = null)
        {
            string fn = string.Empty;

            if (filename != null)
            {
                fn = Path.Combine(Path.GetTempPath(), filename);
            }
            else
            {
                fn = Path.GetTempFileName();
            }
            
            File.WriteAllText(fn, text);
            return fn;
        }

        public static ProjectFileModel CreateProcessedFile(string text)
        {
            var rootDirectory = ProjectOperations.GetRootDirectory();
            var filePath = CreateTempFileWithText(text);
            var file = ProjectOperations.AddExistingFile(filePath, rootDirectory.RowId);
            return file;
        }

        public static string JoinParagraphs(IEnumerable<string> paragraphs)
        {
            string separator = Environment.NewLine + Environment.NewLine;
            return string.Join(separator, paragraphs);
        }

        public static string JoinParagraphs(params string[] paragraphs)
        {
            return string.Join(TextSegmentationService.ParagraphSeparator, paragraphs);
        }

        public static void AssertMultilineStringEqual(string expected, string actual)
        {
            expected = expected.Replace("\r\n", "\n");
            Assert.AreEqual(expected, actual);
        }

        public static void LemmatizeWordsSameAsOriginalWordWithoutLastCharacter()
        {
            var rootDirectory = ProjectOperations.GetRootDirectory();
            var files = rootDirectory.GetFiles();

            if (files.Count == 1)
            {
                var words = LemmatizerOperations.GetWords(files[0]);
                foreach (var word in words)
                {
                    var content = word.GetContent();
                    content = content.Substring(0, content.Length-1);
                    LemmatizerOperations.UpdateBaseFormForWords(content, word);
                }
            }
        }

        public static void AddDefaultPOSTypes()
        {
            ProjectOperations.AddPOSType("Noun", null);
            ProjectOperations.AddPOSType("Verb", null);
            ProjectOperations.AddPOSType("Adjective", null);
            ProjectOperations.AddPOSType("Adverb", null);
            ProjectOperations.AddPOSType("Determiner", null);
            ProjectOperations.AddPOSType("Conjunction", null);
            ProjectOperations.AddPOSType("Preposition", null);
            ProjectOperations.AddPOSType("Interjection", null);
            ProjectOperations.AddPOSType("Particle", null);
            ProjectOperations.AddPOSType("Pronoun", null);
            ProjectOperations.AddPOSType("Numerical", null);
        }   
    }      
}          
           